﻿using habbo.Kernel.Commons;
using habbo.Kernel.Storage.Querys;
using habbo.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace habbo.Habbo.Catalog
{
    class CatalogPage
    {
        /// <summary>
        /// Id of the page
        /// </summary>
        public int Id;

        /// <summary>
        /// Caption of the page
        /// </summary>
        public string Caption;

        /// <summary>
        /// IconColor of the page
        /// </summary>
        public int IconColor;

        /// <summary>
        /// IconImage of the page
        /// </summary>
        public int IconImage;

        /// <summary>
        /// Is the page Visible?
        /// </summary>
        public bool Visible;

        /// <summary>
        /// Layout of the page
        /// </summary>
        public string Layout;

        /// <summary>
        /// ParentId of the page(category)
        /// </summary>
        public int ParentId;

        /// <summary>
        /// The headline of the page
        /// </summary>
        public string PageHeadline;

        /// <summary>
        /// The teaser of the page
        /// </summary>
        public string PageTeaser;

        /// <summary>
        /// Any specialties?
        /// </summary>
        public string PageSpecial;

        /// <summary>
        /// Text1 of page
        /// </summary>
        public string PageText1;
        
        /// <summary>
        /// Text2 of page
        /// </summary>
        public string PageText2;

        /// <summary>
        /// Details of the page
        /// </summary>
        public string PageDetails;

        /// <summary>
        /// Is the page enabled?
        /// </summary>
        public bool Enabled;

        /// <summary>
        /// Second teaser of the page
        /// </summary>
        public string PageTeaser2;

        public List<CatalogItem> Items;

        /// <summary>
        /// Loads all pages.
        /// </summary>
        /// <param name="Row"></param>
        public CatalogPage(DataRow Row)
        {
            using (RowAdapter Adapter = new RowAdapter(Row))
            {
                this.Id = Adapter.PopInt32("id");
                this.Caption = Adapter.PopString("caption");
                this.IconColor = Adapter.PopInt32("icon_color");
                this.IconImage = Adapter.PopInt32("icon_image");
                this.Visible = Adapter.PopBoolean("visible");
                this.Layout = Adapter.PopString("page_layout");
                this.ParentId = Adapter.PopInt32("parent_id");
                this.Enabled = Adapter.PopBoolean("enabled");

                this.PageHeadline = Adapter.PopString("page_headline");
                this.PageTeaser = Adapter.PopString("page_teaser");

                this.PageDetails = Adapter.PopString("page_text_details");
                this.PageTeaser2 = Adapter.PopString("page_text_teaser");
                this.PageSpecial = Adapter.PopString("page_special");
                this.PageText1 = Adapter.PopString("page_text1");
                this.PageText2 = Adapter.PopString("page_text2");
                this.Items = new List<CatalogItem>();


                foreach (DataRow Rowing in SystemApp.MySQLManager.GetObject(new GetCatalogItems(Id)).GetOutput<DataTable>().Rows)
                {
                    using (RowAdapter Adapt = new RowAdapter(Rowing))
                    {
                        if (Adapt.PopString("item_ids") == "" || Adapt.PopInt32("amount") <= 0)
                        {
                            continue;
                        }

                        Items.Add(new CatalogItem(Adapt.PopInt32("id"), Adapt.PopString("catalog_name"), Adapt.PopString("item_ids"), Adapt.PopInt32("cost_credits"), Adapt.PopInt32("cost_pixels"), Adapt.PopInt32("amount")));
                    }
                }
            }
        }

        public bool isEnabled()
        {
            return Enabled;
        }

        public CatalogItem GetItem(int Id)
        {
            foreach (CatalogItem Item in Items)
            {
                if (Item.Id == Id)
                {
                    return Item;
                }
            }

            return null;
        }
    }
}
